/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/24 12:10
 */
package org.mechanic.fund.repository.product;

import org.mechanic.fund.domain.product.Product;
import org.mechanic.fund.domain.product.ProductNetvalue;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

/**
 * 产品净值数据访问接口
 *
 * @author mechanic
 * @version 0.0.1
 */
public interface ProductNetvalueRepository extends PagingAndSortingRepository<ProductNetvalue, String> {
    /**
     * 返回每个产品最新的一条净值记录，并按净值日期降序排序
     *
     * @return 每个产品最新的一条净值记录列表
     */
    @Query(value = "SELECT p FROM ProductNetvalue p WHERE p.newestDate IN (SELECT MAX(newestDate) FROM ProductNetvalue  GROUP BY product)")
    Page<ProductNetvalue> findNewestByProduct(Pageable pageable);

    /**
     * 返回匹配指定产品的净值列表
     *
     * @param product 产品
     * @return 匹配指定产品的净值列表
     */
    Page<ProductNetvalue> findByProductOrderByNewestDateDesc(Product product, Pageable pageable);
}
